package org.xtb;



import org.xtb.utils.ProcessUtil;
import org.xtb.utils.ReadFileUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class SoftwareArchitecture extends JFrame {
    private final JComboBox<String> architectureComboBox;
    private final JTextArea outputTextArea;
    private final JLabel architectureLabel;
    private String fileContent;
    private File file;
    private static final int Window_Width = 1400;
    private static final int Window_Height = 800;

    private static final int width = 500;
    private static final int height = 500;


    public SoftwareArchitecture() {
        setTitle("经典软件体系结构教学软件");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(Window_Width,Window_Height));
//        setLocationRelativeTo(null);

        JButton selectFileButton = new JButton("选择文件");
        JLabel fileNameJLabel = new JLabel("");
        JButton processButton = new JButton("处理");
        JButton saveToFileButton = new JButton("保存到文件");
        JButton clearFileButton = new JButton("移除文件");
        JButton clearTextButton = new JButton("清空内容");
        architectureComboBox = new JComboBox<>(new String[]{"主程序-子程序", "面向对象", "事件系统", "管道-过滤器"});
        outputTextArea = new JTextArea();
        architectureLabel = new JLabel();

        JPanel controlPanel = new JPanel(new FlowLayout());
        controlPanel.add(new JLabel("选择体系结构风格:"));
        controlPanel.add(architectureComboBox);
        controlPanel.add(selectFileButton);
        controlPanel.add(fileNameJLabel);
        controlPanel.add(processButton);
        controlPanel.add(saveToFileButton);
        controlPanel.add(clearFileButton);
        controlPanel.add(clearTextButton);

        JPanel infoPanel = new JPanel(new BorderLayout());
//        infoPanel.setPreferredSize(new Dimension(600,400));
        infoPanel.add(new JLabel("体系结构原理图:"),BorderLayout.NORTH);
        infoPanel.add(architectureLabel);

        JPanel outputPanel = new JPanel(new BorderLayout());
        outputPanel.add(new JLabel("处理结果:"), BorderLayout.NORTH);
        outputTextArea.setFont(new Font(null, Font.PLAIN,18));
        outputPanel.add(outputTextArea);

        add(controlPanel, BorderLayout.NORTH);
        add(infoPanel, BorderLayout.LINE_START);
        add(outputPanel,BorderLayout.CENTER);

        selectFileButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                String filePath = selectedFile.getAbsolutePath();
                try {
                    fileContent = ReadFileUtil.readFile(filePath);
                    outputTextArea.setText(fileContent);
                    fileNameJLabel.setText(selectedFile.getName());
                } catch (IOException ex) {
                    outputTextArea.setText("无法读取文件");
                }
            }
        });

        processButton.addActionListener(e -> {
            String selectedArchitecture = (String) architectureComboBox.getSelectedItem();
            String input = fileContent;
            String output = null;
            if (selectedArchitecture != null) {
                output = processInput(selectedArchitecture, input);
            }
            outputTextArea.setText(output);
        });

        saveToFileButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showSaveDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    String output = outputTextArea.getText();
                    File selectedFile = fileChooser.getSelectedFile();
                    String filePath = selectedFile.getAbsolutePath();
                    try {
                        saveToFile(output, filePath);
                    } catch (IOException ex) {
                        outputTextArea.setText("无法保存文件");
                    }
                }
            }

            private void saveToFile(String content, String filePath) throws IOException {
                Path path = Paths.get(filePath);
                Files.writeString(path, content);
            }
        });

        clearFileButton.addActionListener(e ->{
            fileNameJLabel.setText("");
            fileContent="";
        });

        clearTextButton.addActionListener(e->{
            outputTextArea.setText("");
        });

        pack();
        setVisible(true);
    }

    private String processInput(String architecture, String input) {
        // 根据选择的体系结构风格进行处理，返回处理结果
        // 这里只是一个示例，实际处理过程需要根据具体的体系结构风格进行编写
        switch (architecture) {
            case "主程序-子程序" -> {
                // 主程序-子程序风格的处理逻辑
                String processedInput = ProcessUtil.processLine(input);
                URL url = SoftwareArchitecture.class.getResource("主程序-子程序.png");
                architectureLabel.setIcon(getIcon(url));
                return processedInput;
            }
            case "面向对象" -> {
                // 面向对象风格的处理逻辑
                String processedInput = ProcessUtil.processObject(input);
                URL url = SoftwareArchitecture.class.getResource("面向对象.png");
                architectureLabel.setIcon(getIcon(url,800,400));
                return processedInput;
            }
            case "事件系统" -> {
                // 事件系统风格的处理逻辑
                String processedInput = ProcessUtil.processEvents(input);
                URL url = SoftwareArchitecture.class.getResource("事件系统.png");
                architectureLabel.setIcon(getIcon(url,1000,550));
                return processedInput;
            }
            case "管道-过滤器" -> {
                // 管道-过滤器风格的处理逻辑
                String processedInput = ProcessUtil.processPipeline(input);
                URL url = SoftwareArchitecture.class.getResource("管道-过滤.png");
                architectureLabel.setIcon(getIcon(url,950,600));
                return processedInput;
            }
            default -> {
                return "未知的体系结构风格";
            }
        }
    }

    public static ImageIcon getIcon(URL url,int width,int height){
        ImageIcon icon = null;
        if (url != null) {
            icon = new ImageIcon(url);
        }
        if (icon != null) {
            icon.setImage(icon.getImage().getScaledInstance(width, height,Image.SCALE_DEFAULT));
        }
        return icon;
    }

    public static ImageIcon getIcon(URL url){
        return getIcon(url,width,height);
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(SoftwareArchitecture::new);
    }
}